/*
 * Copyright (c) 2009 Enio Perpetuo
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 *  
 */
package co.za.gvi.transport;

import co.za.gvi.bind.CustomDataType;
import co.za.gvi.bind.ObjectFactory;
import co.za.gvi.bind.RecipientListType;
import co.za.gvi.bind.RecipientListType.Recipient;
import co.za.gvi.example.EmployeeCustomData;
import co.za.gvi.smart.SmartMessage;

/**
 * This class is used to create a recipient list.
 * <p>
 * The <code>RecipientListProxy</code> supports 2 levels of <code>customData</code>:
 * <ul>
 *   <li>as child of <code>recipientList</code> element</li>
 *   <li>as a child of the <code>recipient</code> element</li>
 * </ul> 
 * <p>
 * <b>Note:</b> Because of the way the Grapevine server processes the XML files, the customData 
 * elements MAY NOT have child elements that have the same names. If elements with 
 * the same names exist on all these levels, the lower-level elements will OVERWRITE 
 * the higher-level element and you will lose data.
 * 
 * @author Enio Perpetuo
 */
public class RecipientListProxy {

	private RecipientListType recipientList;
	private ObjectFactory factory;

	/**
	 * Constructor 
	 */
	public RecipientListProxy() {
		this.factory = new ObjectFactory();
		this.recipientList = factory.createRecipientListType();
	}
	
	/**
	 * Get the RecipientListType
	 * 
	 * @return The RecipientListType
	 */
	protected RecipientListType getRecipientListType() {
		return this.recipientList;
	}
	
	/**
	 * Set the message for the recipient list
	 * 
	 * @param message The text message
	 */
	public void setMessage(String message){
		this.recipientList.setMessage(message);
	}

	/**
	 * Set the smart message for the recipient list
	 * 
	 * @param smartMessage The vcard or vcalendar message
	 */
	public void setMessage(SmartMessage smartMessage){
		this.recipientList.setMessage(smartMessage.toString());
	}

	/**
	 * Add a recipient and a Custom Data as child of the recipient element
	 * 
	 * @param customData The custom data to be child of the recipient element
	 * @param msisdn The mobile telephone number 
	 */
	public void addRecipient(EmployeeCustomData customData, String msisdn){
		
		// Creates recipient
		Recipient recipient = this.factory.createRecipientListTypeRecipient();
		recipient.setMsisdn(msisdn);
		
		// Creates custom data
		if(customData != null) {
			recipient.setCustomData(customData);
		}
		this.recipientList.getRecipient().add(recipient);
	}
	
	/**
	 * Add a recipient
	 * 
	 * @param msisdn The mobile telephone number 
	 */
	public void addRecipient(String msisdn){
		addRecipient(null, msisdn);
	}
	
	/**
	 * Add Custom Data at the recipient list level
	 * 
	 * @param customData The custom data to add
	 */
	public void addCustomData(CustomDataType customData){
		this.recipientList.setCustomData(customData);
	}
}